<?php
/**
 * Created by PhpStorm.
 * User: 17732
 * Date: 2018/6/21
 * Time: 10:10
 */

namespace app\admin\model;


use think\Db;
use think\Model;

class Borrow extends Model
{

    /**
     * 根据 states 状态查询出数据
     * @param array $states
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function borrow($states = [''],$order = 'asc',$filed = 'returntime'){
        $query = Db::table('borrow')->alias('b')
            ->join('user','b.uid = user.id')
            ->join('books','b.bid = books.id')
            ->field('b.*,books.bname,books.img,books.from,books.addtime,
            books.bstates,books.isdelete,books.deletereason,books.deletetime,
            user.name,user.account,user.type,user.instime,user.ustates')
            ->where('b.states','in',$states)
            ->where('isdelete','1')
            ->order($filed,$order)
            ->select();
        return $query;
    }
    /**
     * 搜索
     * @param array $states
     * @param $name
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function borrowSearch($states = [''],$name){
        $query = Db::table('borrow')->alias('b')
            ->join('user','b.uid = user.id')
            ->join('books','b.bid = books.id')
            ->field('b.*,books.bname,books.img,books.from,books.addtime,
            books.bstates,books.isdelete,books.deletereason,books.deletetime,
            user.name,user.account,user.type,user.instime,user.ustates')
            ->where('b.states','in',$states)
            ->where('books.bname|user.name','like','%'.$name.'%')
            ->where('isdelete','1')
            ->select();
        return $query;
    }
    /**
     * 更新某个字段的值
     */
    public static function updateBorrowOne($id,$data){
        return Db::table('borrow')->where('id',$id)->update($data);
    }

    /**
     * @param $name
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function searchBorrow($name){
        $query = Db::table('borrow')->alias('b')
            ->join('user','b.uid = user.id')
            ->join('books','b.bid = books.id')
            ->field('b.*,books.bname,books.img,books.from,books.addtime,
            books.bstates,books.isdelete,books.deletereason,books.deletetime,
            user.name,user.account,user.type,user.instime,user.ustates')
            ->where('books.bname|user.name','like',$name)
            ->select();
        return $query;
    }

    public static function selectGroupBy($bid){
        $query = Db::name('borrow')
            ->where('bid',$bid)->select();
        return $query;
    }
}